// 好友模块-mysql服务处理

// 引入数据库模块
const db = require('../config/mysql.js')
const utils = require("../common/utils")

// 发送好友请求
const friendRequest = (uid, fid) => {
  return new Promise((resolve, reject) => {
    let sql1 = `select user_id, friend_id, friend_status from friend where user_id = "${uid}" and friend_id = "${fid}"`
    db.query(sql1, (err1, result1) => {
      if (err1) return reject(err1)
      if (result1.length == 1) {
        // 申请过
        if (result1[0].friend_status == 1) {
          return reject("你已经申请过了,请不要重复申请")
        }
        // 已经是好友
        if (result1[0].friend_status == 2) {
          return reject("你们已经是好友了")
        }
        // 对方先申请,直接加好友
        if (result1[0].friend_status == 0) {
          let time = utils.formatDate(new Date().getTime())
          let sql2 = `update friend set friend_status = 2, know_time = "${time}" where (user_id = "${uid}" and friend_id = "${fid}") or (user_id = "${fid}" and friend_id = "${uid}")`
          db.query(sql2, (err2, result2) => {
            if (err2) return reject(err2)
            if (result2.affectedRows == 2) return resolve("添加成功,已成为好友")
          })
          // 拒绝过(TODO)
        } else {
          // let sql3 = `update friend set friend_status = 1 where user_id = "${uid}" and friend_id = "${fid}";update friend set friend_status = 0 where user_id = "${fid}" and friend_id = "${uid}"`
          let sql3 = `update friend set friend_status = CASE when user_id = "${uid}" and friend_id = "${fid}" then 1 when user_id = "${fid}" and friend_id = "${uid}" then 0 end`
          db.query(sql3, (err3, result3) => {
            if (err3) return reject(err3)
            if (result3.affectedRows == 2) return resolve("申请成功,请等待回应")
          })
        }
      } else {
        // 未申请过
        let sql4 = `insert into friend (user_id, friend_id, friend_status) values("${uid}", "${fid}", 1),("${fid}", "${uid}", 0)`
        db.query(sql4, (err4, result4) => {
          if (err4) return reject(err4)
          if (result4.affectedRows == 2) return resolve("申请成功,请等待回应")
        })
      }
    })
  })
}

// 拒绝好友请求
const rejectFriend = (uid, fid) => {
  return new Promise((resolve, reject) => {
    let sql = `update friend set friend_status = CASE when user_id = "${uid}" and friend_id = "${fid}" then 3 when user_id = "${fid}" and friend_id = "${uid}" then 4 end`
    db.query(sql, (err, result) => {
      if (err) return reject(err)
      if (result.affectedRows == 2) return resolve("已拒绝")
    })
  })
}

// 同意好友请求
const beFriend = (uid, fid) => {
  return new Promise((resolve, reject) => {
    let time = utils.formatDate(new Date().getTime())
    let sql = `update friend set friend_status = 2, know_time = "${time}" where (user_id = "${uid}" and friend_id = "${fid}") or (user_id = "${fid}" and friend_id = "${uid}")`
    db.query(sql, (err, result) => {
      if (err) return reject(err)
      if (result.affectedRows == 2) return resolve("你们已成为好友，开始聊天吧")
    })
  })
}

// 删除好友
const deleteFriend = (uid, fid) => {
  return new Promise((resolve, reject) => {
    let sql = `delete from friend where ((user_id = "${uid}" and friend_id = "${fid}") or (user_id = "${fid}" and friend_id = "${uid}")) and friend_status = 2`
    db.query(sql, (err, result) => {
      if (err) return reject(err)
      if (result.affectedRows == 2) return resolve("删除成功")
      return reject("删除失败")
    })
  })
}

module.exports = {
  friendRequest,
  rejectFriend,
  beFriend,
  deleteFriend
}